// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ChronosWorkerDotNet.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework
{
    #region Using Directives

    using System.ComponentModel;
    using System.Threading;

    #endregion

    /// <summary>
    /// The trtr.
    /// </summary>
    public class ChronosWorkerDotNet : AbtractChronosWorker
    {
        #region Constants and Fields

        /// <summary>
        /// The worker.
        /// </summary>
        private readonly BackgroundWorker worker = new BackgroundWorker();

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ChronosWorkerDotNet"/> class. 
        /// </summary>
        /// <param name="resolution">
        /// The Resolution.
        /// </param>
        /// <param name="precision">
        /// The precision.
        /// </param>
        public ChronosWorkerDotNet()
            : base(30, 15)
        {
            this.worker.DoWork += this.DoWork;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The start.
        /// </summary>
        public override void Start()
        {
            this.worker.RunWorkerAsync();
        }

        /// <summary>
        /// The stop.
        /// </summary>
        public override void Stop()
        {
            this.worker.CancelAsync();
        }

        #endregion

        #region Methods

        /// <summary>
        /// The do work.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void DoWork(object sender, DoWorkEventArgs e)
        {
            Devel.NoThrow(
                delegate
                    {
                        while (true)
                        {
                            this.OnTimer();
                            Thread.Sleep(this.Resolution);
                        }
                    });
        }

        #endregion
    }
}